<?php
namespace app\admin\model;

//users 管理员页
use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;

class Users extends Model{
    protected $pk='id';
    //users  管理员列表页
     public static function   userList(){
         $list=Users::alias('u')
             ->field('u.*,ag.title')
             ->join('ecm_auth_group ag','ag.id=u.auth')
             ->paginate(6);
         return $list;
     }
     //users 管理员添加页
     public static function   userAdd($post){
         if (Request::isPost()){
               Db::startTrans();
             try{
                 $res=Users::insertGetId($post);
                 $access=AuthGroupAccess::insert(['uid'=>$res,'group_id'=>$post['auth']]);
                 Db::commit();
             }catch (Exception $e){
                 $res=false;
                 $access=false;
                 Db::rollback();
             }
             return  [$res,$access];
         }else{
             $auth=AuthGroup::select()->toArray();
             return $auth;
         }
     }
     //users  管理员修改页
     public static function   userEdit($param){
         if (Request::isPost()){
                Db::startTrans();
             try{
                $res=Users::update($param);
                AuthGroupAccess::where('uid','=',$param['id'])->update(['group_id'=>$param['auth']]);
                 Db::commit();
             }catch (Exception $e){
                 $res=false;
                 Db::rollback();
             }
                return $res;
         }else{
             $find=Users::find($param['id'])->toArray();
             $auth=AuthGroup::select()->toArray();
             return [$find,$auth];
         }

         }

     //users  管理员删除页
     public static function   userDel($get){
         Db::startTrans();
        try{
             $res=Users::destroy($get['id']);
            $access=AuthGroupAccess::where('uid','=',$get['id'])->delete();
            Db::commit();

        }catch (Exception $e){
            $res=false;
           $access=false;
            Db::rollback();
        }
        return  [$res,$access];
     }

}